﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;

namespace RssReaderTemplate.Wizard
{
    public class WizardRunnerSelector : WizardBase
    {
        private IWizard _selectedWizard;

        public WizardRunnerSelector()
        {

        }


        public override void BeforeOpeningFile(ProjectItem projectItem)
        {
            _selectedWizard.BeforeOpeningFile(projectItem);
        }

        public override void ProjectFinishedGenerating(Project project)
        {
            _selectedWizard.ProjectFinishedGenerating(project);
        }

        public override void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            _selectedWizard.ProjectItemFinishedGenerating(projectItem);
        }

        public override void RunFinished()
        {
            _selectedWizard.RunFinished();
        }

        public override void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            base.RunStarted(automationObject,replacementsDictionary,runKind,customParams);
            _selectedWizard = SelectWizard(_projectDirectory);
            _selectedWizard.RunStarted(automationObject, replacementsDictionary, runKind, customParams);
        }

        private IWizard SelectWizard(string projectDirectory)
        {
            var wizards = new List<WizardBase>(){new SilentWizardRunner(projectDirectory), new GUIWizardRunner()};
            return wizards.FirstOrDefault(x => x.CanRun);
        }

        public override bool ShouldAddProjectItem(string filePath)
        {
            return _selectedWizard.ShouldAddProjectItem(filePath);
        }
    }
}
